package handler

import (
	"github.com/qiniu/api.v7/auth/qbox"
	"github.com/qiniu/api.v7/storage"
	"hotel_Manager_Api/util"
	"github.com/martini-contrib/encoder"
)

type TokenResult struct {
	Token string `json:"token"`
	//Key string `json:"key"`
}

func GetQiniuPolicy (enc encoder.Encoder,idGenerate util.IdGenerator)[]byte  {
	accessKey := util.Config().MustValue("qiniu","AccessKey")
	secretKey := util.Config().MustValue("qiniu","SecretKey")
	bucketName := util.Config().MustValue("qiniu","BackManagerBucket")
	preFix := util.Config().MustValue("qiniu","UploadPrefix")

	putPolicy := storage.PutPolicy{
		Scope:  bucketName,
		IsPrefixalScope:1,
		SaveKey:preFix+"$(etag)",
	}

	mac := qbox.NewMac(accessKey, secretKey)
	upToken := putPolicy.UploadToken(mac)


	result := TokenResult{Token:upToken}
	return encoder.Must(enc.Encode(result));
}
